﻿using HardcoreGame.Common.UI;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class AgeUISystem : ModSystem
    {
        private UserInterface _ageInterface;
        private AgeUI _ageUI;

        public override void Load()
        {
            _ageUI = new AgeUI();
            _ageInterface = new UserInterface();            
        }

        public override void UpdateUI(GameTime gameTime)
        {
            if (_ageInterface?.CurrentState != null)
            {
                _ageInterface.Update(gameTime);
            }
        }

        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int mouseTextIndex = layers.FindIndex(layer => layer.Name == "Vanilla: Mouse Text");
            if (mouseTextIndex != -1)
            {
                layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer(
                    "HardcoreGame: Age Selection",
                    delegate {
                        if (_ageInterface?.CurrentState != null)
                        {
                            _ageInterface.Draw(Main.spriteBatch, new GameTime());
                        }
                        return true;
                    },
                    InterfaceScaleType.UI)
                );
            }
        }

        public void ShowAgeSelection() => _ageInterface?.SetState(_ageUI);
        public void HideAgeSelection() => _ageInterface?.SetState(null);
    }
}